//
//  Letter.m
//  HotWord
//
//  Created by Rainbow on 8/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Letter.h"
#import "Word.h"

@implementation Letter
@synthesize letter, words,learnIndex,practiceIndex,updateTimestamp;
-(id)init
{
    self = [super init];
    if (self) {
        words = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
	[aCoder encodeObject:self.letter forKey:@"letter"];
	[aCoder encodeObject:self.words forKey:@"words"];
	[aCoder encodeObject:self.learnIndex forKey:@"learnIndex"];
	[aCoder encodeObject:self.practiceIndex forKey:@"practiceIndex"];
	[aCoder encodeObject:self.updateTimestamp forKey:@"updateTimestamp"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	if ([self init]) {
		self.letter= [aDecoder decodeObjectForKey:@"letter"];
		self.words = [aDecoder decodeObjectForKey:@"words"];
		self.learnIndex = [aDecoder decodeObjectForKey:@"learnIndex"];
		self.practiceIndex = [aDecoder decodeObjectForKey:@"practiceIndex"];
		self.updateTimestamp = [aDecoder decodeObjectForKey:@"updateTimestamp"];
    }
	return self;
}

- (void)syncLetterStauts:(Letter *)inLetter
{
    Word *learnWord = [inLetter.words objectAtIndex:[inLetter.learnIndex intValue]];
    Word *practiceWord = [inLetter.words objectAtIndex:[inLetter.practiceIndex intValue]];
    for (Word *word in self.words) {
        if ([self.learnIndex intValue] != 0 && [self.practiceIndex intValue] != 0) {
            return;
        }
        if ([word.wordId isEqualToString:learnWord.wordId]) {
            self.learnIndex = [NSNumber numberWithInt:[self.words indexOfObject:word]];
        }
        if ([word.wordId isEqualToString:practiceWord.wordId]) {
            self.practiceIndex = [NSNumber numberWithInt:[self.words indexOfObject:word]];
        }
    }
}

-(void)dealloc
{
    self.letter = nil;
    self.words = nil;
    self.learnIndex = nil;
    self.practiceIndex = nil;
    self.updateTimestamp = nil;
    [super dealloc];
}

@end
